﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DoeObjects;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DoeLibsTest
{
    [TestClass]
    public class Reservation_Test
    {
        [TestInitialize]
        public void initializeDatabaseConnection()
        {
            TestContext.initialize();
        }

        [TestMethod]
        public void test_getExpiredReservations()
        {
            Title t1 = Helper.createTitle(true);
            Title t2 = Helper.createTitle(true);
            User u1 = Helper.createUserObject(false, true);
            User u2 = Helper.createUserObject(false, true);
            User u3 = Helper.createUserObject(false, true);

            Assert.IsTrue(Helper.clearReservationsTable(), "could not delete old reservations");
            Assert.IsTrue(Helper.clearLoanTable(), "could not delete old loans");

            //there are no reservations
            Assert.AreEqual(0, Reservation.getExpiredReservations().Count);

            //add one expired reservation
            Reservation expiredReservation1 = new Reservation();
            expiredReservation1.AvailableDate = DateTime.Now.AddDays(-7);
            expiredReservation1.LoanRecalled = true;
            expiredReservation1.ReserveDate = DateTime.Now.AddDays(-210);
            expiredReservation1.Title = t1;
            expiredReservation1.User = u1;
            Assert.IsTrue(expiredReservation1.save(), "could not save expiredReservation1");

            Assert.AreEqual(1, Reservation.getExpiredReservations().Count);

            //add another expired reservation
            Reservation expiredReservation2 = new Reservation();
            expiredReservation2.AvailableDate = DateTime.Now.AddDays(-8);
            expiredReservation2.LoanRecalled = true;
            expiredReservation2.ReserveDate = DateTime.Now.AddDays(-21);
            expiredReservation2.Title = t2;
            expiredReservation2.User = u2;
            Assert.IsTrue(expiredReservation2.save(), "could not save expiredReservation2");

            Assert.AreEqual(2, Reservation.getExpiredReservations().Count);

            //add a not expired reservation
            Reservation notExpiredReservation = new Reservation();
            notExpiredReservation.AvailableDate = DateTime.MinValue;
            notExpiredReservation.LoanRecalled = false;
            notExpiredReservation.ReserveDate = DateTime.Now.AddDays(-21);
            notExpiredReservation.Title = t2;
            notExpiredReservation.User = u3;
            Assert.IsTrue(notExpiredReservation.save(), "could not save notExpiredReservation");

            Assert.AreEqual(2, Reservation.getExpiredReservations().Count);

            //delete a expired reservation
            Assert.IsTrue(Reservation.delete(expiredReservation1.ReservationId), "could not delete expiredReservation");

            Assert.AreEqual(1, Reservation.getExpiredReservations().Count);
        }

        [TestMethod]
        public void test_getNextReservation_WithRecalParameterSet()
        {
            User u1 = Helper.createUserObject(false, true);
            User ur = Helper.createUserObject(false,true);

            Loanable l1 = Helper.createLoanable(true);

            Assert.IsTrue(Reservation.createReservation(l1.Title.TitleId, ur.UserId),"Reservation not Created Sucessfuly");//Res

            Reservation testRes = Reservation.getNext(l1.Title.TitleId, true);

            Assert.AreEqual(l1.Title.TitleId, testRes.Title.TitleId);
            Assert.AreEqual(ur.UserId, testRes.User.UserId);
            
        }

        [TestMethod]
        public void test_getNextReservation_WithoutRecalParameterSet()
        {

        }

		[TestMethod]
		public void test_CreateReservation()
		{
			User ur = Helper.createUserObject(false, true);
			Loanable l1 = Helper.createLoanable(true);

			Assert.IsTrue(Reservation.createReservation(l1.Title.TitleId, ur.UserId), "Reservation not Created Sucessfuly");
			Assert.IsFalse(Reservation.createReservation(l1.Title.TitleId, ur.UserId), "Created copy reservation");

			Assert.IsFalse(Reservation.createReservation(1000, 4), "Created reservation without existing id's");// userid or titleid not existing
		}
		
    }
}
